package cn.lijiajia3515.cairo.security.oauth2.resource.server.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * OAuth2 认证异常类
 */
@Slf4j
public class CairoOAuth2HttpMessageAuthenticationFailedHandler implements AuthenticationFailureHandler {
	private final CairoBearerTokenAuthenticationEntryPoint entryPoint;

	public CairoOAuth2HttpMessageAuthenticationFailedHandler(CairoBearerTokenAuthenticationEntryPoint entryPoint) {
		this.entryPoint = entryPoint;
	}

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		log.debug("authentication failed ", exception);
		entryPoint.commence(request, response, exception);
	}
}
